class GoBang {
    /**
     * @description 棋盘
     */
    chessBoard = [[]];
    /**
     * @description 是否玩家回合
     */
    player = true;

    /**
     * @description 游戏是否结束
     */
    isGameOver = false;

    /**
     * @description 游戏获胜方法
     */
    winningMethod = [];

    /**
     * @description 玩家棋子记录
     */
    playerChessTally = [];

    /**
     * @description AI 棋子记录
     */
    aiChessTally = [];

    /**
     * @description 棋盘格数
     */
    frame = 20;

    /**
     * @description 棋盘宽度
     */
    width = 0;

    /**
     * @description 棋盘外边距
     */
    padding = 10;

    constructor(canvas) {
        this.canvas = canvas;
        this.context = this.init_chessboard();
        this.width = this.canvas.width;
    }

    /**
     * @description 初始化棋盘数据
     */
    init_chessBoard() {
        this.chessBoard = Array.from({ length: this.frame }, () => Array.from({ length: this.frame }, () => 0));
    }

    /**
     * @description 初始化游戏获胜方法数据
     */
    init_winningMethod() {
        this.winningMethod = Array.from({ length: this.frame }, () => Array.from({ length: this.frame }, () => Array.from({ length: 0 })));


    }

    /**
     * @description 初始化棋盘
     * @returns context 
     */
    init_chessboard(canvas = this.canvas) {
        const context = canvas.getContext('2d');
        context.strokeStyle = '#000';

        //导入棋盘背景
        const img = new Image();
        img.src = 'images/board.jpg';
        img.onload = () => {
            context.drawImage(img, 0, 0, this.width, this.width);
            //绘制棋盘网格线
            this.draw_chessboard_line(context);
        }

        return context;
    }


    /**
     * @description 绘制棋盘网格线
     * @param {*} cnt 
     */
    draw_chessboard_line(cnt, frame = this.frame, width = this.width, padding = this.padding) {
        const size = (width - 2 * padding) / (frame - 1);
        for (let i = 0; i < frame; i++) {
            cnt.moveTo(i * size + padding, padding);
            cnt.lineTo(i * size + padding, width - padding);
            cnt.stroke();
            cnt.moveTo(padding, padding + i * size);
            cnt.lineTo(width - padding, padding + i * size);
            cnt.stroke();
        }
    }

}